import {
  bindPermission,
  deleteRouter,
  insertOneRouter,
  lookAllRouter,
  lookRouter,
  lookRouterByPermission,
  unbindPermission,
  updateOneRouter,
} from '@/api/router'
import store from '@/store/index'

const getDefaultState = () => {
  return {
    routerList: [],
  }
}

const state = getDefaultState()

const mutations = {
  SET_LIST: (state, routerList) => {
    state.routerList = routerList
  },
}

const actions = {
  // 条件查询
  async lookAllRouter({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 缓存
      if (state.routerList.length > 0) {
        resolve(state.routerList)
        return
      }
      lookAllRouter()
        .then((response) => {
          commit('SET_LIST', response.data)
          resolve(response.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 条件查询
  async lookRouter({ state }, router) {
    return new Promise((resolve, reject) => {
      lookRouter(router)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 添加一条路由
  async insertOneRouter({ commit, state }, router) {
    return new Promise((resolve, reject) => {
      insertOneRouter(router)
        .then((response) => {
          store.dispatch('router/resetList')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 修改路由
  async updateOneRouter({ commit, state }, router) {
    return new Promise((resolve, reject) => {
      updateOneRouter(router)
        .then((response) => {
          store.dispatch('router/resetList')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 批量删除路由
  async deleteRouters({ commit, state }, ids) {
    return new Promise((resolve, reject) => {
      deleteRouter(ids)
        .then((response) => {
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 根据权限ID查询对应路由列表
  async lookRouterByPermission({ state }, permissionId) {
    return new Promise((resolve, reject) => {
      lookRouterByPermission(permissionId)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 绑定权限到路由
  async bindPermission({ state }, map) {
    return new Promise((resolve, reject) => {
      bindPermission(map)
        .then((response) => {
          store.dispatch('router/resetList')
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 解绑权限到路由
  async unbindPermission({ state }, map) {
    return new Promise((resolve, reject) => {
      unbindPermission(map)
        .then((response) => {
          store.dispatch('router/resetList')
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 重置列表
  async resetList({ commit, state }) {
    // 置空
    if (state.routerList.length > 0) {
      commit('SET_LIST', [])
    }
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
